#include "conf.h"
#include "lylib/logger.h"
#include "lylib/iniparser.h"
#include "stdio.h"
#include "logger.h"

int mqttd_parser_conf(const char *conf_file,  mqtt_ctx_t *ctx, int debug)  //删除了日志部分
{
	dictionary	*ini;
	char		*str;
	int		val;

	memset(ctx, 0, sizeof(*ctx));

	if( !conf_file )
	{
		strncpy(ctx, "h098jXhNP9d.mqtt|securemode=2,signmethod=hmacsha256,timestamp=1721637242760|", sizeof(ctx->id)); //这个id没有按照文档的加斜杆

		/*logger settings*/
		strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
		ctx->loglevel = LOG_LEVEL_DEBUG;
		ctx->logsize  = 1024;

		if( logger_init(ctx->logfile, ctx->loglevel, ctx->logsize) < 0)
		{
			printf("logger system  initialise failure\n");
			return -2;
		}	

		log_info("Logger system initialise ok\n");
		log_warn("WARNNING: Use default MQTT configure\n");

		strncpy(ctx->host, DEF_BORKER_HOSTNAME, sizeof(ctx->host));
		ctx->port = DEF_BROKER_PORT;
		log_info("Use default broker server [%s:%d]\n", ctx->host, ctx->port);

		strncpy(ctx->uid, DEF_BROKER_USERNAME, sizeof(ctx->uid));
		strncpy(ctx->pwd, DEF_BROKER_PASSWD, sizeof(ctx->pwd));
		log_info("Use default broker author by [%s:%s]\n", ctx->uid, ctx->pwd);

		ctx->keepalive = DEF_BROKER_KEEPALIVE;
		log_info("Use default broker keepalive timeout [%d] seconds\n", ctx->keepalive);

		/* Publisher settings */
		strncpy(ctx->pubTopic, DEF_PUBTOPIC, sizeof(ctx->pubTopic));
		ctx->pubQos = DEF_PUBQOS;
		ctx->interval = DEF_PUBINTERVAL;
		log_info("Use default publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);

		/* Subscriber settings */
		strncpy(ctx->subTopic, DEF_SUBTOPIC, sizeof(ctx->subTopic));
		ctx->subQos = DEF_SUBQOS;
		log_info("Use default subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);

		return 0;
	}


	ini = iniparser_load(conf_file);
	if( !ini )
	{
		printf("ERROR: connect parse file: '%s'\n", conf_file);
		return -1;
	}

	if( !debug ) 
	{
		str = iniparser_getstring(ini, "logger:file", "mqttd.log");
		strncpy(ctx->logfile, str, sizeof(ctx->logfile));
		ctx->logsize = iniparser_getint(ini, "logger:size", 1024);
		ctx->loglevel = iniparser_getint(ini, "logger:level", LOG_LEVEL_DEBUG);
	}
	else
	{
		strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
		ctx->loglevel = LOG_LEVEL_DEBUG;
		ctx->logsize = 1024;
	}

	if( logger_init(ctx->logfile, ctx->loglevel, ctx->logsize) < 0 ) 
	{
		printf("Logger system initialise failure\n");
		return -2;
	}

	log_info("Logger system initialise ok\n");


	if(!(str = iniparser_getstring(ini, "broker:hostname", NULL)))
	{
		log_err("ERROR Parser broker server hostname failure\n");
		return -2;
	}

	strncpy(ctx->host, str, sizeof(ctx->host) );

	if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 )
	{
		log_err("ERROR: Parser broker server port failure\n");
		return -2;
	}
	ctx->port = val;
	log_info("Parser broker server [%s:%d]\n", ctx->host, ctx->port);

	str=iniparser_getstring(ini, "broker:username", DEF_BROKER_USERNAME);
	strncpy(ctx->uid, str, sizeof(ctx->uid) );

	str=iniparser_getstring(ini, "broker:password", DEF_BROKER_PASSWD);
	strncpy(ctx->pwd, str, sizeof(ctx->pwd) );
	
	log_info("Parser broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
	
	str=iniparser_getstring(ini, "broker:client_id",DEF_BROKER_CLIENT_ID);
	strncpy(ctx->clientId, str, sizeof(ctx->clientId));

	ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_BROKER_KEEPALIVE);
	log_info("Parser broker keepalive timeout [%d] seconds\n", ctx->keepalive);
	/*+------------------------------------------------------+
	 *|             parser publisher settings                |
	 *+------------------------------------------------------+*/
	str=iniparser_getstring(ini, "publisher:pubTopic", DEF_PUBTOPIC);
	strncpy(ctx->pubTopic, str, sizeof(ctx->pubTopic) );

	ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_PUBQOS);
	ctx->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL);
	log_info("Parser publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);

	/*+------------------------------------------------------+
	 *|             parser subscriber settings               |
	 *+------------------------------------------------------+*/

	str=iniparser_getstring(ini, "subsciber:subTopic", DEF_SUBTOPIC);
	strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) );

	ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_SUBQOS);
	log_info("Parser subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);

	return 0;

}
